package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfChannelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoChannelInfo;
import com.ocom.payhandle.service.XfChannelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class XfChannelInfoServiceImpl extends ServiceImpl<IDaoChannelInfo, EntityXfChannelInfo> implements XfChannelInfoService {


	@Autowired
	private IDaoChannelInfo iDaoChannelInfo;

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public EntityXfChannelInfo pickChannel(Long companyId, String device_no,Integer merch_id) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",companyId);
		queryWrapper.and(wrapper -> wrapper.eq("device_no", device_no).or().eq("device_no", "0"));
		queryWrapper.and(wrapper -> wrapper.eq("merch_id", merch_id).or().eq("merch_id", 0));
		List<EntityXfChannelInfo> list=this.list(queryWrapper);
		if(list==null || list.size()==0){
			return null;
		}
		return list.get(0);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	@Override
	public List<EntityXfChannelInfo>  selectPayWay(Long comId, Long merchId) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("pay_way","pay_type");
		queryWrapper.eq("com_id",comId);
		queryWrapper.eq("merch_id",merchId);
		queryWrapper.last(" ORDER BY update_time desc");   //and  SUBSTRING(pay_type,4,1) = '1'
		List<EntityXfChannelInfo> xfChannelInfos = iDaoChannelInfo.selectList(queryWrapper);

		if(xfChannelInfos==null || xfChannelInfos.size() == 0 ){
			return xfChannelInfos;
		}
		xfChannelInfos = xfChannelInfos.stream().filter(e ->
				e.getPayType().length()>=4 && e.getPayType().substring(3,4).equals("1") ).collect(Collectors.toList());
		return xfChannelInfos;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	public List<EntityXfChannelInfo> selectPayWay(Long comId) {
		QueryWrapper<EntityXfChannelInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("merch_id");
		queryWrapper.eq("com_id",comId);
		return this.list(queryWrapper);
	}
}
